import random
from telnetlib import EC

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
import time
import requests
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

class YZ(object):
    def __init__(self):
        '''
        指定位置chromedriver.exe
        '''
        chrome_option  = self.set_start_option()
        self.driver = webdriver.Chrome(options=chrome_option ,
                                       executable_path='chromedriver.exe')
        self.wait = WebDriverWait(self.driver,10)
    def set_start_option(self):
        '''
        设置浏览器大小
        :return:
        '''
        option = Options()
        option.add_argument('--window-size=1500,1000')
        option.add_argument('--disable-infobars')
        # option.add_argument('--headless')#无界面调试
        # option.headless = True
        return option
    def login_page(self):
        '''
        获取登录页面，分析页面节点，输入手机号密码
        :return:
        '''
        # self.driver.get("https://www.x315.com/")
        # time.sleep(2)
        # #首页获取登录节点
        # self.driver.find_element_by_class_name('user-register').click()
        # time.sleep(1)

        self.driver.get('https://login.x315.com/user/login')
        time.sleep(3)
        self.driver.find_element_by_id('phone').send_keys('15095882178')  # 输入用户名
        self.driver.find_element_by_id('pwd').send_keys('tan15095882178')  # 输入密码
        source = self.driver.find_element_by_id("nc_1_n1z")  # 需要滑动的元素
        # if source:
        #     time.sleep(2)
        action = ActionChains(self.driver)
        action.click_and_hold(source).perform()  # 鼠标左键按下不放
        #调用轨迹
        for i in self.track(272):
            action.move_by_offset(i,0).perform()
        action.release().perform()  # 释放鼠标
        time.sleep(2)
        self.driver.find_element_by_id('loginsubmit').click()  # 点击登录
        time.sleep(3)
    def track(self,distance):
        t = 0.2
        mid = 3 / 5 * distance    #匀减速
        current = 0               #当前位置
        speed = 0                 # 速度
        track_list = []
        while current < distance:
            if current < mid:
                a = 5
            else:
                a = -4
            move_track = speed * t + 0.5 * a * t * t #位移公式
            track_list.append(round(move_track))
            speed += (a * t)
            current += move_track
        #假装超出轨迹
        et_range = 10
        end_track = [1] * et_range +[0] * 6 + [-1,0] * et_range
        track_list.extend(end_track)
        #判断移动距离是否超出
        offset = sum(track_list) - distance
        if offset > 0:
            track_list.extend(offset * [-1,0])
        elif offset < 0:
            track_list.extend(offset * [1,0])
        print(track_list)
        return track_list

    def run(self):
        try:                     #报错后，强行关闭浏览器
            self.login_page()
        finally:
            time.sleep(5)
            self.driver.quit()

if __name__ == '__main__':
    s = YZ()
    s.run()

